<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


class Projekt extends CI_Controller {
	
	function Index() {	// nacita model, template a stranku po overeni prihlasenia
		//$this->load->model('projekt_model'); // potrebny pre pracu s DB
		if ($this->session->userdata('logged_in')) {
		
			redirect('projekt/logged');
		
		}
		
		$this->load->model('projekt_model');
		$value = $this->projekt_model->show_table('2014');//date('Y'));
		$data['tabulka'] = $value;
		$this->load->view('index_view.php', $data);		
		
	}
	
	function logged() {	
		
		$this->load->model('projekt_model');
		$temy = $this->projekt_model->GetAllMisions();
		$data['themes'] = $temy;
		$zad_id = array();
		foreach ($temy as $obj) {
			if (($obj->end > $obj->start) && ($obj->start <= date("Y-m-d H:i:s")) && ($obj->start >= date ("Y"))) {
			
				array_push($zad_id,$obj->id);
			}
		}
		
		$data['zad_id'] = $zad_id;
		$value = $this->projekt_model->show_table('2014');
		$data['tabulka'] = $value;
		$sols = $this->projekt_model->GetAllSolutions();
		$data['solutions'] = $sols;
		$this->load->view('logged_view.php',$data);
		
	}
		
	function testing() {
		return 5;
	}	

	function show_mission($tid) {
		
		
		$this->load->model('projekt_model');
		$this->load->helper('date');
		
		$temy = $this->projekt_model->GetMissions($tid);
		$data['zadanie'] = $temy;
		
		$mid = $tid;

		$this->session->set_flashdata('mid', $mid);
		
		$sols = $this->projekt_model->GetSolutions($mid);
		
		$data['riesenie'] = $sols;
		
		$users = $this->projekt_model->GetSolutions($mid);
		$data['riesenie'] = $sols;

		$data['obrazky'] = array_diff(scandir('./attachments'), array('..', '.'));
		
		$this->load->view('zadanie.php',$data);
		
		
	}

	
	function show_solution($sid) {
		$this->load->model('projekt_model');
		$this->load->model('download_model');
		
		////////////////////////////////////// Vytiahni riešenie podľa jeho IDčka
		
		$sols = $this->projekt_model->GetOneSolution($sid);
		$data['riesenie'] = $sols;
		
		////////////////////////////////////// Vytiahni komentáre k riešeniu ak nejaké sú
		
		$komentar = $this->projekt_model->GetComments($sid);
		$data['komentar'] = $komentar;
		$comments = '';
		foreach($komentar as $obj) {
		
			$comments .= $obj->text."\n\n";
		
		}
		if (count($komentar) > 0) {
			$data['komentare'] = $komentar[0]->text;
		}
		else {
			$data['komentare'] = '';
		}
		
		////////////////////////////////////// Vytiahne hodnotenia prihláseného uživateľa podľa IDčak riešenia
		
		$res_data['user_id']  = $this->session->userdata('id');
		$res_data['sol_id'] = $sid;
		$hodnotenia = $this->projekt_model->GetMyResults($res_data);
		$hodnotenia = $hodnotenia->result();
		$hodnotenia = (count($hodnotenia));
		$data['pocet_hodnoteni'] = $hodnotenia;
		$links = $this->download_model->get_links($sid);
		$prilohy = $this->download_model->get_attachments($sid);

		$data['links'] = $links;		
		$data['attachments'] = $prilohy;

		$data['obrazok'] = $this->download_model->get_img($sid);

		$this->load->view('riesenia.php',$data);	
		
		
	}
	
	function evalu($sol_id) {

		$this->load->model('projekt_model');
		$user_id = $this->session->userdata('id');
		$data['sol_id'] = $sol_id;
		$data['user_id'] = $user_id;
				
		if ($this->projekt_model->add_eval($data)) {
			
			redirect("projekt/show_solution/{$sol_id}");
			
		
		}
		
		else {
			print_r('Dakde sa stala chyba :C');
		}
	
	}
	
	function Publ($mid) {
		
		$data['mid'] = $mid;
		$this->load->model('projekt_model');
		$data['zverejnit'] = 0;
		if ($_POST['publish'] == '4Jablka') {
			$data['zverejnit'] = 1;
		}
		if ($this->projekt_model->publ($data)) {
			
			redirect("projekt/show_mission/{$mid}");
			
		}
	
	}
	
	function win($sol_id) {

		if (!$this->session->userdata('logged_in')) {
		
			redirect('projekt/logged');
		
		}

		$this->load->model('projekt_model');
		if ($this->projekt_model->win($sol_id)) {
			
			redirect("projekt/show_solution/{$sol_id}");	
		
		}
		else {
			echo 'CHYBA pri vkladaní do databázy';
		}
	}
	
	
	function lose($sol_id) {

		if (!$this->session->userdata('logged_in')) {
		
			redirect('projekt/logged');
		
		}

		$this->load->model('projekt_model');
		if ($this->projekt_model->lose($sol_id)) {
			
			redirect("projekt/show_solution/{$sol_id}");	
		
		}
		else {
			echo 'CHYBA pri vkladaní do databázy';
		}
	}	
	
	function update_time($mid) {
		$this->load->model('projekt_model');
		if ($this->projekt_model->update_time($mid)) {
			
			redirect("projekt/show_mission/{$mid}");	
		
		}
		else {
			echo 'CHYBA pri vkladaní do databázy';
		}
	}

	function load_table($year) {
		$this->load->model('projekt_model');
		
	}
	
	function edit_mission($mid) {
	
		$this->load->model('projekt_model');
		if ($this->projekt_model->edit_mission_cont($mid)) {
			
			redirect("projekt/show_mission/{$mid}");	
		
		}		
	}

	function do_upload()
	{	

		if (!$this->session->userdata('logged_in')) {
		
			redirect('projekt/logged');
		
		}
		
		$config['upload_path'] = './attachments';
		$config['allowed_types'] = 'gif|jpg|png';

		$this->load->library('upload', $config);

		if ( ! $this->upload->do_upload() )
		{	

			$this->load->model('projekt_model');
			$this->load->helper('date');

			$mid = $this->session->flashdata('mid');
		
			$temy = $this->projekt_model->GetMissions($mid);
			$data['zadanie'] = $temy;
		

			$this->session->set_flashdata('mid', $mid);
		
			$sols = $this->projekt_model->GetSolutions($mid);
		
			$data['riesenie'] = $sols;
		
			$users = $this->projekt_model->GetSolutions($mid);
			$data['riesenie'] = $sols;
			$data['obrazky'] = array_diff(scandir('./attachments'), array('..', '.'));
			$data['error'] = $error = $this->upload->display_errors();
		
			$this->load->view('zadanie.php',$data);

		}
		else
		{	
			$obrazok = $this->upload->data();

			chmod('./attachments/'.$obrazok['file_name'], 0755);

			$this->load->model('projekt_model');
			$this->load->helper('date');

			$mid = $this->session->flashdata('mid');
		
			$temy = $this->projekt_model->GetMissions($mid);
			$data['zadanie'] = $temy;
		

			$this->session->set_flashdata('mid', $mid);
		
			$sols = $this->projekt_model->GetSolutions($mid);
		
			$data['riesenie'] = $sols;
		
			$users = $this->projekt_model->GetSolutions($mid);
			$data['riesenie'] = $sols;
			$data['obrazky'] = array_diff(scandir('./attachments'), array('..', '.'));
			$data['error'] = 'Obrázok úspešne nahraný';
		
			$this->load->view('zadanie.php',$data);
	
		}
	}

	function delete_attachment($img)
	{
		unlink('./attachments/'.$img);
		$this->load->model('projekt_model');
		$this->load->helper('date');

		$mid = $this->session->flashdata('mid');
	
		$temy = $this->projekt_model->GetMissions($mid);
		$data['zadanie'] = $temy;
	

		$this->session->set_flashdata('mid', $mid);
	
		$sols = $this->projekt_model->GetSolutions($mid);
	
		$data['riesenie'] = $sols;
	
		$users = $this->projekt_model->GetSolutions($mid);
		$data['riesenie'] = $sols;
		$data['obrazky'] = array_diff(scandir('./attachments'), array('..', '.'));
		$data['error'] = 'Obrázok úspešne vymazaný';
	
		$this->load->view('zadanie.php',$data);
	}


}
?>
